package com.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.mall.constant.StatusConstant;
import com.mall.dto.CategoryDTO;
import com.mall.dto.CategoryPageQueryDTO;
import com.mall.po.Category;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.CategoryService;
import com.mall.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-04-18
 */
@RestController
@RequestMapping("/admin/category")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增分类")
    public Result saveCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类: {}", categoryDTO);
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        category.setStatus(StatusConstant.DISABLE);
        categoryService.save(category);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> categoryPageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类分页查询: {}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.categoryPageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询分类")
    public Result<CategoryVO> getCategoryById(@PathVariable Long id) {
        log.info("根据id查询分类: {}", id);
        Category category = categoryService.getById(id);
        CategoryVO categoryVO = BeanUtil.copyProperties(category, CategoryVO.class);
        return Result.success(categoryVO);
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping("update")
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类: {}", categoryDTO);
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        categoryService.updateById(category);
        return Result.success();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("根据id删除分类")
    public Result delCategoryById(Long id) {
        log.info("根据id删除分类: {}", id);
        categoryService.delCategoryById(id);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用分类")
    public Result updateCategoryStatus(@PathVariable Integer status, Long id) {
        log.info("启用、禁用分类: id: {}, status: {}", id, status == 1 ? "启用" : "禁用");
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryService.updateById(category);
        return Result.success();
    }

    /**
     * 查询启用状态的分类
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询启用状态的分类")
    public Result<List<Category>> getCategoryByStatus() {
        log.info("查询启用状态的分类");
        List<Category> categoryList = categoryService.lambdaQuery()
                .eq(Category::getStatus, StatusConstant.ENABLE)
                .list();
        return Result.success(categoryList);
    }
}
